package com.fcat.common.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fcat.springboot.mybatis.service.impl.TsuantoneBaseServiceImpl;
import com.fcat.base.common.util.ArrayUtil;
import com.fcat.springboot.mybatis.util.JsonUtil;
import com.fcat.base.common.util.StrUtil;
import com.fcat.common.user.entity.Organization;
import com.fcat.common.user.entity.User;
import com.fcat.common.user.mapper.OrganizationMapper;
import com.fcat.common.user.service.OrganizationService;
import com.fcat.common.user.service.RoleService;
import com.fcat.common.user.service.UserService;
import com.fcat.common.user.vo.*;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrganizationServiceImpl extends TsuantoneBaseServiceImpl<OrganizationMapper, Organization> implements OrganizationService {

    @Autowired
    private UserService userService;
    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private RoleService roleService;

    @Override
    public JSONObject getTreeByUsername(String username) {
        User user = userService.getUserByUsername(username);
        if (user == null) return JsonUtil.getFailJsonObject("当前用户不存在");
        Map<String, Object> map = new HashMap<>();
        map.put("id", user.getOrganizationId());
        if("super".equals(username)) map.put("id", 0);
        List<OrganizationVO> organizationVOList = getOrganizationTree(map);
        return JsonUtil.getSuccessJsonObject(organizationVOList);
    }

    @Override
    public List<OrganizationVO> getOrganizationTree(Map<String, Object> map) {
        return mapper.getOrganizationTree(map);
    }

    @Override
    public List<Long> getIdsByPrentId(Long organizationId) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", organizationId);
        List<OrganizationIdVO> deptVOList = mapper.getChildrenIdById(map);
        List<Long> longList = new ArrayList<>();
        getAllChildrenId(longList, deptVOList);
        longList.add(organizationId);
        return longList;
    }

    @Override
    public List<OrganizationSearchVO> search(String keyword,Long companyId,String username) {
        Map<String,Object> map=new HashMap<>();
        map.put("keyword", StrUtil.isBlank(keyword)?"":keyword);
        map.put("companyId",companyId);
        map.put("username",username);
        return mapper.search(map);
    }

    @Override
    public List<OrganizationVO> getTreeByCompanyId(Long companyId,String username) {
        Map<String,Object> map=new HashMap<>();
        map.put("companyId",companyId);
        map.put("username",username);
        List<Organization> list=mapper.getListByCompanyId(map);
        List<OrganizationVO> organizationVOList=new ArrayList<>();
        for (Organization organization : list) {
            OrganizationVO parentOrganizationVO=getParentNode(organization.getParentId(),organizationVOList);
            if(parentOrganizationVO!=null){
                List<OrganizationVO> tmpList=parentOrganizationVO.getChildren();
                OrganizationVO organizationVO=new OrganizationVO();
                organizationVO.setId(organization.getId());
                organizationVO.setTitle(organization.getName());
                organizationVO.setParentId(organization.getParentId());
                organizationVO.setExpand(true);
                tmpList.add(organizationVO);
            }else{
                OrganizationVO organizationVO=new OrganizationVO();
                organizationVO.setId(organization.getId());
                organizationVO.setTitle(organization.getName());
                organizationVO.setParentId(organization.getParentId());
                organizationVO.setExpand(true);
                organizationVO.setChildren(new ArrayList<>());
                organizationVOList.add(organizationVO);
            }
        }
        return organizationVOList;
    }


    @Override
    public JSONObject searchByIdAndType(Long id, String type,Long companyId,String username) {
        JSONObject result=new JSONObject();
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("type",type);
        map.put("username",username);
        map.put("companyId",companyId);
        if("user".equals(type)){
            UserVO userVO=userService.getUserVOById(map);
            result.put("list",userVO);
        }else if ("role".equals(type)){
            Integer count=roleService.getUserCountByRoleId(map);
            result.put("list",count);
        }else if("organizaiton".equals(type)){
            List<Long> longs = organizationService.getIdsByPrentId(id);
            map.put("list",longs);
            List<UserVO> list=userService.getListByOrganizationId(map);
            result.put("list",list);
        }
        return result;
    }

    OrganizationVO getParentNode(Long parentId,List<OrganizationVO> organizationVOList){
        for (OrganizationVO organizationVO : organizationVOList) {
            if(parentId.longValue()==organizationVO.getId().longValue()){
                return organizationVO;
            }
            OrganizationVO tmp=getParentNode(parentId,organizationVO.getChildren());
            if(tmp!=null){
                return tmp;
            }
        }
        return null;
    }

    /**
     * 获取所有的自己Id
     *
     * @param longList
     * @param deptVOList
     */
    public void getAllChildrenId(List<Long> longList, List<OrganizationIdVO> deptVOList) {
        if (!ArrayUtil.isEmpty(deptVOList)) {
            for (OrganizationIdVO vo : deptVOList) {
                longList.add(vo.getId());
                getAllChildrenId(longList, vo.getOrganizationIdVOS());
            }
        }
    }


}